package com.csust.cg.mongodb.service.impl;

import com.csust.cg.mongodb.domain.HttpStatus;
import com.csust.cg.mongodb.domain.Result;
import com.csust.cg.mongodb.domain.entity.Connect;
import com.csust.cg.mongodb.domain.vo.ConnectionVO;
import com.csust.cg.mongodb.exception.ConnectionException;
import com.csust.cg.mongodb.mapper.ConnectMapper;
import com.csust.cg.mongodb.service.RecentConnectService;
import com.csust.cg.mongodb.utils.MongoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chenguang
 */
@Service
public class RecentConnectServiceImpl implements RecentConnectService {

    @Autowired
    private ConnectMapper connectMapper;

    @Override
    public Result addRecentConnect(ConnectionVO connection, String userId) {
        Connect connect = new Connect();
        connect.setUpdateTime(new Date());
        connect.setUserId(userId);
        // 最近
        connect.setType(true);
        connect.setProtocol(connection.getProtocol());
        connect.setIp(connection.getIp());
        connect.setPort(connection.getPort());
        connect.setAuth(connection.getAuth());
        if (connection.getAuth()) {
            connect.setUsername(connection.getUsername());
            connect.setPassword(connection.getPassword());
            connect.setAuthDatabase(connection.getAuthDatabase());
            connect.setAuthMechanism(connection.getAuthMechanism());
        }
        try {
            connectMapper.insertSaveConnect(connect);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统保存连接错误，请联系开发人员！");
        }
        return Result.success();
    }

    @Override
    public Result getAllRecentConnectList(String userId) {
        List<Connect> connects;
        try {
            connects = connectMapper.selectAllRecentConnectListByUserIdOrderByCreateTime(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统获取最近连接错误，请联系开发人员！");
        }
        return Result.success(connects);
    }

    @Override
    public Result addRecentConnectByConnectionId(String connectionId, String userId) {
        ConnectionVO connection = MongoUtils.getConnection(connectionId);
        if (connection == null) {
            throw new ConnectionException("连接ID已失效", HttpStatus.CONNECT_ID_INEFFECTIVE);
        }
        return addRecentConnect(connection, userId);
    }

    @Override
    public Result removeRecentConnects(String userId, ArrayList<Integer> ids) {
        try {
            connectMapper.deleteByUserIdAndIds(userId, ids);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统删除连接错误，请联系开发人员！");
        }
        return Result.success();
    }

    @Override
    public Result removeAllRecentConnect(String userId) {
        try {
            connectMapper.deleteAllByUserIdAndType(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统删除连接错误，请联系开发人员！");
        }
        return Result.success();
    }
}
